package com.qiyu.mq.standalone;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @Author idea
 * @Date created in 10:31 下午 2020/8/7
 */
public class Consumer {

    private static long count=0;

    public static void main(String[] args) throws MQClientException, InterruptedException {
        //默认会有个消费者组名称
        DefaultMQPushConsumer consumer=new DefaultMQPushConsumer("consumer1");
        consumer.setVipChannelEnabled(false);
        consumer.setNamesrvAddr("i.kun.yun.com:9876");
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_TIMESTAMP);
        consumer.subscribe("Qiyu-Topic","*");
        consumer.setConsumeMessageBatchMaxSize(10);
        System.out.println("启动rocketmq");
        CountDownLatch countDownLatch=new CountDownLatch(1);
        //同时接收
        consumer.registerMessageListener(new MessageListenerOrderly() {
            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
                System.out.println("【消费记录】------------------------------------第"+(count)+"次消费");
                for (MessageExt msg : msgs) {
                    System.out.println(msg.toString());
                }
                return ConsumeOrderlyStatus.SUCCESS;
            }
        });
        consumer.start();
    }
}
